import _ from 'lodash'
import { createDB } from '../db/index.js'
const db = await createDB('user-info', 'object')
await db.read()


const userInfoData = db.chain.get('data')

export const userInfoSchema = {
    nickname: { type: 'string', required: true },
    gender: { type: 'number', required: false },
    // avatar_url: { type: 'string', required: false },
    // area: { type: 'array', itemType: 'number', required: false },
    // birthday: { type: 'string', required: false },
    // brief: { type: 'string', required: false}
}

export const UserInfo = {
    async findById(id) {
        const data = userInfoData.value()
        return data[id] || {}
    },
    async create(id, body) {
        const data = userInfoData.value()
        data[id] = body
        await db.write()
        return body
    },
    async update(id, body) {
        const data = userInfoData.value()
        Object.assign(data[id],  body)
        await db.write()
        return body
    },
    async updateAvatar(id, url) {
        const data = userInfoData.value()
        if(!data[id]) {
            data[id] = {
                avatarUrl: url
            }
        } else {
            data[id].avatarUrl = url
        }
        await db.write()
    }
}